package com.junzhiit.nengji.cms.web.controller;


import java.util.List;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.util.zip.ZipUtils;
import com.junzhiit.nengji.cms.web.vo.DownloadFileVO;
import com.junzhiit.nengji.cms.web.vo.DownloadVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/download")
public class DownloadController {
	
	@Value("${upload.data.temp}")
	private String tempPath;
	
	@Value("${upload.mall.shop.table.qrcode}")
	private String qrCodePath;
	
	@ApiOperation(value = "下载图片")
	@RequestMapping(method = RequestMethod.POST)
	public void download(HttpServletResponse response,HttpServletRequest request,String filefullPath){
		AliyunOSSClientUtil.flowDownload(response, filefullPath);
	}
	
	
	@ApiOperation(value = "下载打包文件")
	@RequestMapping(value="/zip",method = RequestMethod.POST)
	public void downloadZip(HttpServletResponse response,DownloadVO downloadVO){
		List<DownloadFileVO> list=JsonUtil.toListObject(downloadVO.getFilePaths(), DownloadFileVO.class);
		
		String zipName = downloadVO.getZipName()+".zip";
		response.setContentType("application/octet-stream; charset=utf8");
		response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
		response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename="+zipName);
        ZipOutputStream out = null;
        try {
        	out=new ZipOutputStream(response.getOutputStream());
    		for(DownloadFileVO downloadFileVO:list){
    			String suffix = downloadFileVO.getFileName().substring(downloadFileVO.getFileName().lastIndexOf("."));
    			AliyunOSSClientUtil.download(qrCodePath+downloadFileVO.getFileName(), tempPath, downloadFileVO.getName()+suffix);
    			ZipUtils.doCompress(tempPath+downloadFileVO.getName()+suffix, out);
    		}
    		out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	
	
}
